import urllib,urllib2,cookielib
import re
BOKEE_HOST='.bokee.com'

class Bokee:
    def __init__(self):
        self.UserName=''
        self.Password=''
        self.cookie=cookielib.CookieJar()
        self.opener=urllib2.build_opener(urllib2.HTTPCookieProcessor(self.cookie))
        urllib2.install_opener(self.opener)
    def login(self,username,password):
        
        self.bokeeFile="bokee.xml"
        self.UserName=username
        self.BlogPath="http://"+self.UserName+BOKEE_HOST

        _str=urllib.urlencode({'username':username,'password':password})
        _response=urllib2.urlopen('http://reg.bokee.com/account/LoginCtrl.b',_str)
        _d=_response.read()
        if (re.search('bokie', _d)):
            return True
        else:
            return False
        
    
    def PostIt(self,_title,_content):
        self.opener=urllib2.build_opener(urllib2.HTTPCookieProcessor(self.cookie))
        urllib2.install_opener(self.opener)
        _req=urllib2.Request(url=self.BlogPath+'/control/doPostDiary.b')
        _req.add_header('Content-Type','text/xml;charset=GBK')
        _data=open('bokee.xml','r').read()
        _data=_data.replace('$#content#$',_content)
        _data=_data.replace('$#title#$',_title)
        _req.add_data(_data)
        _r=urllib2.urlopen(_req)
        #_d=_r.read()
        if (re.search(r'SUCCESS',_r.read())):
            return True
        else:
            return False

